interface RequestInterface<T = any> {
  code: number;
  message: string;
  data: T;
  z?: string;
}

interface RequestOptionsInterface<T> {
  type?: 'ok' | 'error' | 'warning';
  message?: string;
  data?: T;
}

type R<T = any> = (opts: RequestOptionsInterface<T>) => RequestInterface<T>;

export const r: R = (opts) => {
  opts.type = opts.type || 'ok';
  let code = 200;
  let message: string = 'success';
  switch (opts.type) {
    case 'ok':
      code = 200;
      message = 'success';
      break;
    case 'warning':
      code = 400;
      message = 'warning';
      break;
    case 'error':
      code = 500;
      message = 'error';
      break;
    default:
      break;
  }
  message = opts.message || message;
  opts.data = opts.data ? opts.data : {};
  return {
    code,
    message,
    data: opts.data,
    z: 'xfxj',
  };
};
